package com.hgl.listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;




import com.hgl.service.ConfigService;
import com.hgl.service.impl.ConfigServiceImpl;
import com.hgl.utils.ELTools;


@WebListener
public class SystemListener implements HttpSessionListener,ServletContextListener{
    private static Logger log=LoggerFactory.getLogger(SystemListener.class);
	private int count;
	private int online;
	private ServletContext application;
    ConfigService service=new ConfigServiceImpl();
	@Override
	
	public void contextInitialized(ServletContextEvent sce) {
		count=service.getCount();
        System.out.println(service.getCount());
		application=sce.getServletContext();
		application.setAttribute("count", count);
		application.setAttribute("online", online);
		//开始九添加创建一个全局给EL表达式使用的工具对象
		application.setAttribute("eltool", new ELTools());
	}
	


	@Override
	public void sessionCreated(HttpSessionEvent se) {
		count++;
		online++;
		application.setAttribute("count", count);
		application.setAttribute("online", online);

	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		//保存现在的访问人数
		service.saveCount(count);
	}
	@Override
	public void sessionDestroyed(HttpSessionEvent se) {
		online--;
		application.setAttribute("online", online);
	}

}
 